package com.huazhi.wzsnw.exportData;

import com.huazhi.wzsnw.mapper.LinedMapper;
import com.huazhi.wzsnw.mapper.VillageMapper;
import com.huazhi.wzsnw.mapper.XzMapper;
import com.huazhi.wzsnw.model.Lined;
import com.huazhi.wzsnw.model.Village;
import com.huazhi.wzsnw.model.Xzzd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

/**
 * @author zhengch
 * @date 2021年08月16日 5:02 下午
 * 数据整合工具类
 */
@Component
public class ExportDataUtil {

@Autowired
private XzMapper xzMapper;

@Autowired
private LinedMapper linedMapper ;

@Autowired
private VillageMapper villageMapper;

/*
 *  将设备的村镇数据导入数据字典表中
 * @author zhengch
 * @date 2021/8/16 5:06 下午
 * @param null
 * @return null
 */
public  void importNameFromLine (){
    List<Lined> list = linedMapper.getAll();
    System.out.println("设备乡镇开始导入------------------");
    for (Lined lined: list){
        String cun = lined.getCun();
        String cwh = lined.getCunweihui();
        String zhen = lined.getZhen();
        if(!checkName(zhen,cwh,cun)){
            System.out.println("此条数据不符合规范！！！");
            continue;
        }
        if(!have(zhen)){
         //不含此镇
         Xzzd xzzd = new Xzzd();
         xzzd.setPid(-1);
         xzzd.setTitle(zhen);
         xzMapper.insert(xzzd);
        }else if(!have(cwh)){
         //不含此村委会
          int pid ;
          Xzzd xzzd = new Xzzd();
          xzzd.setTitle(cwh);
          pid = getPid(zhen);
          xzzd.setPid(pid);
          xzMapper.insert(xzzd);
        }else if(!have(cun)){
        //不含此自然村
           int pid;
           Xzzd xzzd = new Xzzd();
           pid = getPid(cwh);
           xzzd.setPid(pid);
           xzzd.setTitle(cun);
           xzMapper.insert(xzzd);
            Village village = new Village();
            village.setTid(UUID.randomUUID().toString());
            village.setName(cun);
            village.setCunweihui(cwh);
            village.setZhen(zhen);
            village.setCreatetime(new Date());
            village.setHushu(0);
            villageMapper.insertSelective(village);
        }

    }

    System.out.println("设备乡镇导入完成----------------------");
}
/*
 *  判断乡镇字典是否含有这个（村，村委会，镇）
 * @author zhengch
 * @date 2021/8/16 5:09 下午
 * @param null
 * @return null
 */

private  boolean have(String name){
    List<Xzzd> list = xzMapper.selectByTitle(name);
    return  list.size()>0 ? true:false;

}
/*
 * 获取父id
 * @author zhengch
 * @date 2021/8/16 5:21 下午
 * @param null
 * @return null
 */
private  int getPid(String name){
   List<Xzzd> list = xzMapper.selectByTitle(name);
   return  list.get(0).getId();

}

/*
 *  检查是否符合规范
 * @author zhengch
 * @date 2021/8/23 9:39 上午
 * @param null
 * @return null
 */
public  boolean checkName(String zhen,String cwh,String cun){
   if(zhen == null || "".equals(zhen)){
      return  false;
   }
   if(cwh == null || "".equals(cwh) ){
       return  false;
   }

   if(cun == null || "".equals(cun)){
       return  false;
   }

   return  true;
}


}
